/**
 * 
 */
package org.log4jconfig.xml.editor.contentasssit;

import java.net.URL;

import junit.framework.Assert;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.launching.JavaRuntime;
import org.junit.Test;
import org.log4jconfig.xml.editor.Log4jEditorPlugin;
import org.osgi.framework.Bundle;

/**
 * @author Xi
 * 
 */
@SuppressWarnings("restriction")
public class TypeHierarchyTestCase {

	@Test
	public void testRetrieveAppenderHierarchy() throws Exception {
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IProject project = root.getProject("TestLog4j");
		Assert.assertNotNull("Project not found", project);
		/*
		if (!project.exists()) {
			project.create(null);
			project.open(null);
			addJavaNature(project);
			//createBinFolder(project);
			IJavaProject javaProject = JavaCore.create(project);
			addProjectLibraries(javaProject);
			addSystemLibraries(javaProject);
		}
		*/
		Assert.assertTrue("Project is not a JavaProject", JavaProject
				.hasJavaNature(project));

		IJavaProject nature = (IJavaProject) project
				.getNature("org.eclipse.jdt.core.javanature");
	
		IType type = nature.findType("org.apache.log4j.Appender", (IProgressMonitor)null);
		Assert.assertNotNull("Type not found", type);

	}

	private void addProjectLibraries(IJavaProject javaProject)
			throws CoreException {
		Bundle plugin = Platform.getBundle(Log4jEditorPlugin.PLUGIN_ID);
		URL url = plugin.getResource("lib/log4j-1.2.14.jar");
		IPath libPath = new Path(plugin.getLocation() + url.getFile());
		
		IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
		IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
		System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
		newEntries[oldEntries.length] = JavaCore.newContainerEntry(libPath);
		javaProject.setRawClasspath(newEntries, null);
	}

	private void addSystemLibraries(IJavaProject javaProject)
			throws JavaModelException {
		IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
		IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
		System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
		newEntries[oldEntries.length] = JavaRuntime
				.getDefaultJREContainerEntry();
		javaProject.setRawClasspath(newEntries, null);
	}

	private IFolder createBinFolder(IProject project) throws CoreException {
		IFolder binFolder = project.getFolder("classes");
		binFolder.create(false, true, null);
		return binFolder;
	}

	private void addJavaNature(IProject project) throws CoreException {
		IProjectDescription description = project.getDescription();
		description.setNatureIds(new String[] { JavaCore.NATURE_ID });
		project.setDescription(description, null);

	}
}
